package com.operate;

public class Method {

	public static int byteToInt(byte[] b) {
		if(b.length == 3){
			return ((b[0] & 0xFF) << 16) | ((b[1] & 0xFF) << 8) | b[2] & 0xFF;
		}else{
			return ((b[0] & 0xFF) << 24) | ((b[1] & 0xFF) << 16) |((b[2] & 0xFF) << 8) | b[3] & 0xFF;
		}	
	}
	
	public static byte[] intToByte(int n) {
		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
			b[i] = (byte) (n >> (24 - i * 8));
		}
		return b;
	}

	public static String bytes2HexString(byte[] b) {
		String ret = "";
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			ret += hex.toUpperCase();
		}
		return ret;
	}

	public static String bytestoHexString(byte[] b, int len) {
		String ret = "";
		for (int i = 0; i < len; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			ret += hex.toUpperCase();
		}
		return ret;
	}

}
